{% extends "base.html" %}
{% load i18n %}

{% block title %} 
{% if source == 'pc' %}
{% trans "[TITLE] Upload from my computer" %} - Mr. Edu
{% else %}
{% trans "[TITLE] Upload from the Web" %} - Mr. Edu
{% endif %}
{%endblock%}

{% block section%}

	{% if source == 'pc' %}
	<h1>{% trans "[TITLE] Upload from my computer" %}</h1>
	{% else %}
	<h1>{% trans "[TITLE] Upload from the Web" %}</h1>
	{% endif %}
	
	{% if collections %}
	<form id="uploadFile" onsubmit="return loading()" action="/mrEdu/{{userRole}}/content/upload/from/{{source}}/" method="post" accept-charset="UTF-8" enctype="multipart/form-data">
		<fieldset>
			<legend>{% trans "[LEGEND] Content details" %}</legend>
			<p class="borderTop">
				{% if feedback%}
				<div id="feedback">
					{% for item in feedback %}
					{{item|safe}}
					{% endfor %}
				</div>
				{% endif %}
				<label>{% trans "[LABEL] Name" %}: <input class="text" type="text" id="fileName" name="name" value="{{formData.name}}"></label><br>
				
				{% if source == 'pc' %}
				<label>{% trans "[LABEL] File" %}: <input class="file" type="file" id="file" name="file"></label><br>
				{% else %}
				<label>{% trans "[LABEL] URL" %}: <input class="text" type="text" id="file" name="file" value="{{formData.file}}"></label><br>
				{% endif %}
				
				<label>{% trans "[LABEL] Description" %}:
					<textarea cols="30" rows="2" id="fileDescription" name="description">{{formData.description}}</textarea>
				</label><br>
				
				<label>{% trans "[LABEL] Add to" %}:
					<select id="fileCollection" name="collection">
						<option value="none">{% trans "[LABEL] Choose a folder" %}</option>
						{% for item in collections %}
						{% if item.id == formData.collection.id %} 
						<option value="{{item.id}}" selected="selected">{{item.name}}</option>
						{% else %}
						<option value="{{item.id}}">{{item.name}}</option>
						{% endif %}
						{% endfor %}
					</select>
				</label><br>
				
				<label>{% trans "[LABEL] Tags" %}: <input class="text" type="text" id="fileTags" name="tags" value="{% for item in formData.tags %}{% if not forloop.last %}{{item}}, {% else %}{{item}}{% endif %}{% endfor %}"></label>
			</p>
			<input class="button submit" type="submit" id="submit" value="{% trans "[BUTTON] Save" %}">
			{% csrf_token %}
		</fieldset>
	</form>
	{% else %}
		{% if userRole == 'teacher' %}
	<div id="feedback"><p class="message">{% trans "[MESSAGE] Before uploading your contents, you have to" %} <a href="/mrEdu/{{userRole}}/class/choose">{% trans "[MESSAGE] Create a folder" %}</a>. {% trans "[MESSAGE] What are you waiting for?" %}</p></div>
		{% else %}
	<div id="feedback"><p class="message">{% trans "[MESSAGE] You have to wait that your teacher creates a folder." %}</p></div>
		{% endif %}
	{% endif %}
{%endblock%}

{% block script%}
	<script type="text/javascript">
		function loading()
			{
				var form = document.getElementById('uploadFile');
				var submit = document.getElementById('submit');
				submit.parentNode.removeChild(submit);
				
				var loading = document.createElement('p');
				loading.setAttribute('style', 'text-align: right; font-size: 1.2em')
				loading.innerHTML = 'Carico ...';
				form.appendChild(loading);
				
				form.submit();
				
				return false;
			}
	</script>
{% endblock %}